/* 院区及分组方案配置 */
jQuery(document).ready(function() {
			// 查询院区数据
			var hospData=[];
			$.post(contextPath + "/auth/currentUser/pagingQueryHospital.dhc", {
				pagesize : 10000000,
				page : 0
			}).done(function(result) {
				if (result.data && result.data.length > 0) {
					hospData = result.data;
					var hospCode = $.getUrlParam('hospCode');
					var hospName = decodeURI($.getUrlParam('hospName'));
					if (hospCode != null) {
						selectedHospCode = hospCode;
						selectedHospName = hospName;
					} else {
						var hospCodeArr = [];
						var hospNameArr = [];
						for ( var i in hospData) {
							hospCodeArr.push(hospData[i].hospCode);
							hospNameArr.push(hospData[i].hospShortName);
						}
						selectedHospCode = hospCodeArr.join(',');
						selectedHospName = hospNameArr.join(',');
					}
				}
			});
			$.ajax({
				type : "GET",
				url : contextPath + "/pages/auth/many-switch.jsp",
				async : false,
				success : function(result) {
					switchDialog = new SwitchDialog($(result));
					switchDialog.dialog.find('#toggle').click(function() {
						switchDialog.callbackInvoke();
					});
					switchDialog.dialog.on({
						'shown.bs.modal' : function() {
							switchDialog.loadDataInvoke();
						}
					});
				}
			});
		//	switchDialog.dialog.find('#loading_images').fadeIn(0);
			
			switchDialog.add({
				title : '选择院区',
				icon : 'hospital-o',
				size : 'sm',
				content : '<div class="selectRoleGrid"></div>',
				loadData : function() { 
					// 弹窗初始化完毕后，初始化url选择表格
					drgUI.bindAjaxStart(0);// 暂时去掉模态框
					var columns = [ {
						title : "简称",
						width : 'auto',
						name : "hospShortName"
					}, {
						title : "代码",
						width : 'auto',
						name : "hospCode",
						hidden : true
					} ];
					switchDialog.dialog.find('.selectRoleGrid').grid({
						identity : 'hospCode',
						columns : columns,
						isShowPages : false,
						isUserLocalData : true,
						pageSize : 10000000,
						pageNo : 0,
						pagination : false,
						localData : hospData
					});
					/* 院区表格里内容是否选中状态 */
					/* 获取浏览器里的hospcode */
					var hospTimer = setInterval(function() {
						if (switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').isLoaded()) { // grid没有回调函数可用，暂用此方法判断表格是否初始化完成
							clearInterval(hospTimer);
							drgUI.bindAjaxStart(1);
							/* 检测院区个数 */
							hospCnt = switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').getAllItems().length;
							var hospCode = $.getUrlParam('hospCode');
							var hospName = decodeURI($.getUrlParam('hospName'));
							if (hospCode != null) {
								selectedHospCode = hospCode;
								selectedHospName = hospName;
								var codeArray = hospCode.split(',');
								// checkbox状态修改
								for ( var o in codeArray) {
									var codeIndex = switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').getIndexByIdentityValue(codeArray[o]);
									var codeTr = switchDialog.dialog.find('.grid-table-body table tr').eq(codeIndex);
									$(codeTr).addClass('success');
									$(codeTr).find('td .checkerbox ').addClass('checked');
								}
							} else {
								var hospCode = [];
								var hospName = [];
								var codeTr = switchDialog.dialog.find('.grid-table-body table tr');
								$(codeTr).addClass('success');
								switchDialog.dialog.find('.grid-table-body table tr').find('td .checkerbox ').addClass('checked');
								switchDialog.dialog.find('.grid-table-head table tr').find('th .checkerbox ').addClass('checked');
								var gridData = switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').selectedRows();
								for ( var i in gridData) {
									hospCode.push(gridData[i].hospCode);
									hospName.push(gridData[i].hospShortName)
								}
								selectedHospCode = hospCode.join(',');
								selectedHospName = hospName.join(',');
							}
						}
					}, 100);
				},
				callback : function() {
				}
			});
			switchDialog.add({
				title : '指标计算口径',
				icon : 'puzzle-piece',
				size : 'sm',
				content : '<label>分组方案：&nbsp;</label>' + '<select id="groupPlan" class="form-control" style="width:150px;display:inline">' + '</select><br>'
						+ '<label>标杆类型：&nbsp;</label>' + '<select id="stdType" class="form-control" style="width:150px;display:inline"></select>',
				loadData : function() {
					drgUI.bindAjaxStart(0);// 暂时去掉模态框
					switchDialog.dialog.find('#loading_images').css('display','block');
					//点击分组方案事件
					switchDialog.dialog.find('#groupPlan').on('change',function(){
						//根据分组方案获取对应的标杆类型
						getStdTypeForGroup($(this).val(),function(stdTypeByGroup){
							setTypeByGroup(switchDialog,diction.AS_STD_TYPE,stdTypeByGroup);
						});
					});
					//点击标杆类型事件
					switchDialog.dialog.find('#stdType').on('change',function(){
						//修改值 给出提示
						if($(this).find("option:selected").hasClass("stdtypeNo")){
							//选中第一个 
							switchDialog.dialog.find('#stdType  option:first').prop("selected", 'selected'); 
							drgUI.warning('标杆数据不存在请重新选择!');
						}
					});
					// 获取配置好的分组方案
					$.post(contextPath + "/systemConfig/getSystemGroupScheme.dhc").done(function(result) {
						if (result.success) {
							// 获取当前数据分组方案类型，用来决定分组方案下来框
							$.post(contextPath + "/systemConfig/getMrGroupScheme.dhc").done(function(data) {
								if (data.success && data.data) {
									data = data.data
									var groupScheme = result.data;
									var dicStdType = diction.AS_STD_TYPE;
									var strp = "";
									var strt = "";
									if (groupScheme != null) {
										var gr = groupScheme.split('θ');
										/* 根据首页含有的分组方案随机变化下拉狂 */
										if (data.length == 1) {
											if (gr[0] == 'BJ') {
												strt = '<option value="BJ" selected="selected" >北京版</option>';
											} else {
												strt = '<option value="CN" selected="selected">CC版</option>';
											}
										} else {
											if (gr[0] == 'BJ') {
												strt = '	<option value="BJ" selected="selected" >北京版</option>' + '	<option value="CN">CC版</option>';
											} else {
												strt = '	<option value="BJ" >北京版</option>' + '	<option value="CN" selected="selected">CC版</option>';
											}
										}
										//根据分组方案获取对应的标杆类型
										getStdTypeForGroup(gr[0],function(stdTypeByGroup){
											//用户已选中的标杆类型
											setTypeByGroup(switchDialog,dicStdType,stdTypeByGroup,strt,gr[1]);
										});
									} else {
										if (data.length == 1) {
											var verRegion = data[0].VER_REGION
											if (verRegion == 'BJ') {
												strt = '<option value="BJ" >北京版</option>';
											} else {
												strt = '<option value="CN">CC版</option>';
											}
											//根据分组方案获取对应的标杆类型
											getStdTypeForGroup(verRegion,function(stdTypeByGroup){
												setTypeByGroup(switchDialog,dicStdType,stdTypeByGroup,strt);
											});
										} else {
											strt = '<option value="BJ" >北京版</option>' + '	<option value="CN">CC版</option>';
											//根据分组方案获取对应的标杆类型
											getStdTypeForGroup('BJ',function(stdTypeByGroup){
												setTypeByGroup(switchDialog,dicStdType,stdTypeByGroup,strt);
											});
										}
									}
								}
							});
						}
						// 将解绑的ajaxstart时间重新添加
						drgUI.bindAjaxStart(1);
						switchDialog.dialog.find('#loading_images').css('display','none');
					});
				},
				callback : function() {
					drgUI.bindAjaxStart(0);// 暂时去掉模态框
					/* 分组方案回调 */
					var groupPlan = $.trim(switchDialog.dialog.find('#groupPlan').val());// 测算用分组方案
					var stdType = $.trim(switchDialog.dialog.find('#stdType').val());// 测算用标杆类型
					if (stdType == "" || stdType.length <= 0) {
						drgUI.warning('请设置DRG指标计算选用的标杆类型!');
						return;
					}
					var groupSchemeBenchType = groupPlan + "θ" + stdType;
					/* 设置分组方案显示 */
					setGroupScheme(groupSchemeBenchType);
					/* 院区选择 */
					var items = switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').selectedRows();
					var allItems = switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').getAllItems();
					if (items.length == 0) {
						drgUI.warning('请选择数据!');
						return;
					}
					var hospCode = [];
					var hospName = [];
					var gridData = switchDialog.dialog.find('.selectRoleGrid').data('koala.grid').selectedRows();
					switchDialog.dialog.remove();
					for ( var i in gridData) {
						hospCode.push(gridData[i].hospCode);
						hospName.push(gridData[i].hospShortName);
					}
					selectedHospCode = hospCode.join(',');
					selectedHospName = hospName.join(',');
					// 保存分组方案
					$.post(contextPath + "/systemConfig/setSystemGroupScheme.dhc", {
						userId : $("#userCount_id").val(),
						roleId : $("#roleName_id").val(),
						groupScheme : groupSchemeBenchType
					}).done(function(result) {
						if (result.success) {
							if (hospCnt == 1 || items.length == hospCnt) {
								window.location.href = contextPath + "/index.dhc";
							} else {
								window.location.href = contextPath + "/index.dhc?hospCode=" + selectedHospCode + "&hospName=" + encodeURI(encodeURI(selectedHospName));

							}
						}
					});
					// 将解绑的ajaxstart时间重新添加
					drgUI.bindAjaxStart(1);
				}
			});
			/* 鼠标进入该区域触发 */
			var systips;
			$('#setSysConfig').mouseenter(function() {
				systips = layer.tips('分析院区：' + selectedHospName + '<br>分组方案：' + selectedGroupScheme + '<br>标杆类型：' + selectedStdType, '#setSysConfig', {
					tips : [ 1, 'rgb(23, 24, 25)' ],
					time : 900000
				});
			});
			/* 鼠标离开该区域 */
			$('#setSysConfig').mouseleave(function() {
				layer.close(systips);
			});
			$('#setSysConfig').click(function() {
				switchDialog.show();
			});
			drgUI.tips();
	});

/* 分组方案配置显示 */
function setGroupScheme(groupScheme) {
	var gr = groupScheme.split('θ');
	if (gr[0] == "BJ") {
		selectedGroupScheme = "北京版";
	} else {
		selectedGroupScheme = "CC版";
	}
	groupSchemaConfig.selectedGroupScheme = gr[0] ;
	var dicStdType = diction.AS_STD_TYPE;
	if (dicStdType) {
		for ( var i in dicStdType) {
			if (dicStdType[i].DICVALUE == gr[1]) {
				selectedStdType = dicStdType[i].DICDESC;
				groupSchemaConfig.selectedStdType = dicStdType[i].DICVALUE ;
			}
		}
	}
}
/**
 * @ Discription:获取分组器对应的标杆类型
 * @Autor ：liuhongjun
 * @Date  ：2018年4月18日下午8:28:40
 */
function getStdTypeForGroup(groupSchemeBenchType,callBack){
	$.post(contextPath + "/systemConfig/getGroupByScheme.dhc", {
		groupScheme : groupSchemeBenchType
	}).done(function(result) {
		if (result.success&&result.data) {
			var group=result.data;
			callBack(groupMatchStd[group]);
		}else{
			callBack("");
		}
	});
}
/**
 * @ Discription:设置标杆类型
 * @Autor ：liuhongjun
 * @Date  ：2018年4月19日上午9:40:23
 */
function setTypeByGroup(switchDialog,stdType,stdTypeByGroup,strt,configured){
	if (stdType) {
		var strp="";
		strp += "<option value=''>请选择</option>"
		for ( var i in stdType) {
			var stdtypeYesNo='stdtypeYes';
			if($.inArray(stdType[i].DICVALUE, stdTypeByGroup)<0){
				stdtypeYesNo="stdtypeNo";
			}
			var selected = "";
			if (configured && stdType[i].DICVALUE == configured) {
				selected = "selected='selected'";
			}
			strp += "<option " + selected + " value='" + stdType[i].DICVALUE + "' class='"+stdtypeYesNo+"'>" + stdType[i].DICDESC + "</option>"
		}
	}
	if(strt){
		switchDialog.dialog.find('#groupPlan').html(strt);
	}
	switchDialog.dialog.find('#stdType').html(strp);
}
